




//
//  MBTableView.m
//  canScroll
//
//  Created by MB__Lee on 2019/2/28.
//  Copyright © 2019年 MB__Lee. All rights reserved.
//

#import "MBTableView.h"
@interface MBTableView()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,assign)BOOL canScroll;

@end
static NSString *cell_identi = @"cell";
@implementation MBTableView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        self.delegate = self;
        self.dataSource = self;
        
//        监听父视图（scrollview）滚动到一定偏移量
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeScroll:) name:@"atTop" object:nil];
    }
    return  self;
}

#pragma mark --UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  30;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identi];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_identi];
        cell.textLabel.text = @"doubi";
        cell.detailTextLabel.text = @"xuting";
    }
    
    return  cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  40;
}

#pragma mark ---悬停关键代码： 通知+ canScroll+ scrollview代理方法 + 手势协议
-(void)changeScroll:(NSNotification *)noti{
    _canScroll = YES;
    
}
#pragma mark ---scrollview
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint mboffset = scrollView.contentOffset;
    CGFloat y = mboffset.y;
    
    if (_canScroll == YES) {
        if (y < 0) {
            mboffset.y = 0;
            [scrollView setContentOffset:mboffset];
            _canScroll = NO;
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"subAtZero" object:nil];
        }else{
            
            [scrollView setContentOffset:mboffset];
        }
        
        
    }else{
        [scrollView setContentOffset:CGPointZero];
    }
    
   
    
}





@end
